export default {
  btn () {
    let getcode = this.refs.getcode.value
    let tel = this.refs.username.value
    fetch('http://10.8.165.23:8080/api/users/sendCode', {
      tel: 'tel'
    })
    .then(res => res.json())
    .then(data => {
      console.log(data, tel)
      let num = data.data
      if (num === getcode) {
        return 'success'
      } else {
        alert("验证码错误，请重试")
        // this.props.history.go()
      }
    })
    .catch(err=> {
      console.log(err, 1)
    })
  },
  change () {
    let user = this.refs.username.value
    if (/^1(3|4|5|7|8)\d{9}$/.test(user)) {
      this.setState({
        tel: "手机号码格式正确"
      })
      return 'success'
    } else {
      this.setState({
        tel: "手机号码格式有误"
      })
      return 'error'
    }
  },
  changepass () {
    let pw = this.refs.password.value
    if (/^[0-9A-Za-z]{6,}$/.test(pw)) {
      this.setState({
        pass: "密码格式正确"
      })
      return 'success'
    } else {
      this.setState({
        pass: "密码格式有误"
      })
      return 'error'
    }
  },
  register () {
    if (this.btn === 'success' && this.change === 'success' && this.changepass=== 'success') {
      let formData = {
        username: this.refs.username.value,
        password: this.refs.password.value
      }
      fetch('http://10.8.165.23:8080/api/users/register',{
        method: "POST", 
        headers: { 
          "Content-Type": "application/json"
          }, 
          body: JSON.stringify(formData)
      })
      .then(res => res.json())
      .then(data =>{
        console.log(data)
        console.log(this.props)
        if (data.code === 1) {

          alert("注册成功")
          this.props.history.push('/login')
        } else {
          alert("用户已存在，请重新注册")
          // this.props.history.go()
        }
        
      })
    } else {
      alert("请完成注册")
    }
  } 
}